package framework.view.components.text
{
import framework.core.FontLib;

import flash.events.Event;
import flash.filters.BitmapFilterQuality;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.text.AntiAliasType;
import flash.text.TextField;
import flash.text.TextFormat;

import framework.view.components.IComponent;

public class Text extends TextField implements IComponent
{
	public function Text()
	{
		super();
		this.embedFonts = true;
		this.antiAliasType = AntiAliasType.ADVANCED;
		format = this.defaultTextFormat;
		format.font = FontLib.UI_FONT_NAME;
		this.defaultTextFormat = format;
		this.setTextFormat(format);
	}
	
	
	protected var format:TextFormat;
	
	protected var _align:String;

	public function get align():String
	{
		return _align;
	}

	public function set align(value:String):void
	{
		_align = value;
		format.align = value;
		
		commitProperty();
	}
	
	
	private var _leteterBold:Boolean = false;

	public function get leteterBold():Boolean
	{
		return _leteterBold;
	}

	public function set leteterBold(value:Boolean):void
	{
		_leteterBold = value;
		this.filters = this.filters.concat([new GlowFilter(0x1B1C1C,1,0,0,2)]);
	}

	
	protected var _size:int;
	
	public function get size():int
	{
		return _size;
	}
	
	public function set size(value:int):void
	{
		_size = value;
		format.size = value;
		
		commitProperty();
	}
	
	protected var _autoLayout:Boolean = false;
	
	public function get autoLayout():Boolean
	{
		return _autoLayout;
	}
	
	public function set autoLayout(value:Boolean):void
	{
		_autoLayout = value;
		
		if (value)
		{
			if (this.parent)
			{
				this.width = this.parent.width;
				this.height = this.textHeight + 4;
				
				this.x = 0;
				this.y = (this.parent.height - this.height) / 2;
			}
			else
			{
				this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			}
		}
	}
	
	protected var _autoTextSize:Boolean = false;

	public function get autoTextSize():Boolean
	{
		return _autoTextSize;
	}

	public function set autoTextSize(value:Boolean):void
	{
		_autoTextSize = value;
		
		if (value)
		{
			this.width = this.textWidth + 4;
			this.height = this.textHeight + 4;
		}
	}
	
	protected var _wrap:Boolean;
	public function set wrap(value:Boolean):void
	{
		_wrap = value;
		super.wordWrap = _wrap;
		commitProperty();
	}
	
	public function get wrap():Boolean
	{
		return _wrap;
	}
	
	override public function set textColor(value:uint):void
	{
		super.textColor = value;
		
		format.color = value;
		commitProperty();
	}
	
	override public function set text(value:String):void
	{
		super.text = value;
		autoLayout = _autoLayout;
		autoTextSize = _autoTextSize;
	}
    
    protected var _font:String;

    public function get font():String
    {
        return _font;
    }

    public function set font(value:String):void
    {
        _font = value;
        format.font = value;
        commitProperty();
    }
	
	protected var _hasFilter:Boolean;
	
	public function set hasFilter(value:Boolean):void
	{
		_hasFilter = value;
		if (value)
		{
			var gs:GlowFilter = new GlowFilter(0x1b1c1c, 1, 3, 3, 10, BitmapFilterQuality.LOW);
			this.filters = [ gs ];
		}
		else
		{
			this.filters = null;
		}
	}
	
	public function get hasFilter():Boolean
	{
		return _hasFilter;
	}
	
	protected var _hasTitleFilter:Boolean;
	
	public function set hasTitleFilter(value:Boolean):void
	{
		_hasTitleFilter = value;
		if(value)
		{
			var gf:GlowFilter = new GlowFilter(0x496562, 1, 3, 3, 10, BitmapFilterQuality.LOW);
			var dds:DropShadowFilter = new DropShadowFilter(1.5,60, 0x2D4349, 1, 5, 5 , 4, BitmapFilterQuality.LOW);
			this.filters = [ gf, dds];
		}
		else
		{
			this.filters = null;
		}
	}
	
	public function get hasTitleFilter():Boolean
	{
		return _hasTitleFilter;
	}
	
	private var _hasWeddingTitleFilter:Boolean = false;

	public function get hasWeddingTitleFilter():Boolean
	{
		return _hasWeddingTitleFilter;
	}

	public function set hasWeddingTitleFilter(value:Boolean):void
	{
		_hasWeddingTitleFilter = value;
		if(value)
		{
			var gf:GlowFilter = new GlowFilter(0x302E2F, 1, 5, 5, 50, BitmapFilterQuality.LOW);
			var dds:DropShadowFilter = new DropShadowFilter(1,90, 0x759B95, 1, 4, 4 ,20, BitmapFilterQuality.LOW);
			this.filters = [ gf, dds];
		}
		else
		{
			this.filters = null;
		}
	}
	
	private var _hasDescFilter:Boolean = false;
	
	public function get hasDesFilter():Boolean
	{
		return _hasDescFilter;
	}
	
	public function set hasDesFilter(value:Boolean):void
	{
		_hasDescFilter = value;
		if(value)
		{
			var gf:GlowFilter = new GlowFilter(0xF8FAFA, 1, 4, 4, 10, BitmapFilterQuality.LOW);
			var dds:DropShadowFilter = new DropShadowFilter(1,90, 0x8AAAA6, 1, 2, 2 ,5, BitmapFilterQuality.LOW);
			this.filters = [ gf, dds];
		}
		else
		{
			this.filters = null;
		}
	}


	//==========================================================================
	//  Public methods
	//==========================================================================
	public function commitProperty():void
	{
		this.defaultTextFormat = format;
		this.setTextFormat(format);
		
		autoTextSize = _autoTextSize;
	}
	
	public function dispose():void
	{
		this.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function addToStageHandler(event:Event):void
	{
		this.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		autoLayout = _autoLayout;
	}
	
}
}


